package cn.changeforyou.web.nutz.common;

import java.lang.annotation.Annotation;
import java.util.Map;
import java.util.Map.Entry;
import javax.validation.Valid;
import javax.validation.ValidationException;
import org.nutz.mvc.ActionContext;
import org.nutz.mvc.impl.processor.AbstractProcessor;

/**
 * @author zhyu
 * @date 2021/3/9 15:55
 */
public class ValidatorChain extends AbstractProcessor {

    @Override
    public void process(ActionContext ac) throws Throwable {
        Object param = getValidParam(ac);
        if(null != param) {
            Map<String, String> map = BeanValidator.validateObject(param);
            if (!map.isEmpty()) {
                int size = map.size();
                StringBuilder sb = new StringBuilder();
                if(size > 1) {
                    sb.append("{");
                }
                for (Entry<String, String> entry : map.entrySet()) {
                    if(size > 1) {
                        sb.append("[");
                    }
                    sb.append("参数[").append(entry.getKey()).append("]").append(entry.getValue());
                    if(size > 1) {
                        sb.append("]");
                    }
                }
                if(size > 1) {
                    sb.append("}");
                }
                throw new ValidationException(sb.toString());
            }
        }
        doNext(ac);
    }

    /**
     * 获取Valid注解的参数
     *
     * @param ac
     * @return
     */
    private Object getValidParam(ActionContext ac) {
        Annotation[][] parameterAnnotations = ac.getMethod().getParameterAnnotations();
        if (null != parameterAnnotations) {
            for (int i = 0; i < parameterAnnotations.length; i++) {
                Annotation[] annotation = parameterAnnotations[i];
                if (null != annotation) {
                    for (int j = 0; j < annotation.length; j++) {
                        Annotation an = annotation[j];
                        if (an.getClass() == Valid.class) {
                            Object[] methodArgs = ac.getMethodArgs();
                            if(null != methodArgs[i]) {
                                return methodArgs[i];
                            }else {
                                throw new ValidationException("参数没传递");
                            }
                        } else {
                            Class<?>[] interfaces = an.getClass().getInterfaces();
                            if (null != interfaces) {
                                for (int k = 0; k < interfaces.length; k++) {
                                    Class<?> anInterface = interfaces[k];
                                    if (anInterface == Valid.class) {
                                        //主要走的是这里  因为注解都被代理了
                                        Object[] methodArgs = ac.getMethodArgs();
                                        if(null != methodArgs[i]) {
                                            return methodArgs[i];
                                        }else {
                                            throw new ValidationException("参数没传递");
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
        return null;
    }
}
